import { Controller, Get, Query, Session } from '@nestjs/common';
import { LogService } from './log.service';

@Controller('log')
export class LogController {
  constructor(private readonly logService: LogService) { }

  @Get('getLog')
  async getLog(@Query() query: any, @Session() session: Record<string, any>) {
    if (!session.userId) {
      return {
        code: 1,
        data: null,
        msg: '未登录'
      };
    }

    try {
      console.log('查询日志，参数:', query, '用户ID:', session.userId);

      // 使用消息服务来获取日志记录
      const logs = await this.logService.findAll({
        ...query,
        userId: session.userId
      });

      console.log(`查询到 ${logs.length} 条日志记录`);

      return {
        code: 0,
        data: logs,
        msg: 'success'
      };
    } catch (error) {
      console.error('查询日志失败:', error);
      return {
        code: 1,
        data: null,
        msg: error.message
      };
    }
  }
}